package com.v1.ourdiary.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weibo4j.model.User;

import com.v1.util.Constant;
import com.v1.util.Toolkit;
import com.v1.web.action.BaseDwrAction;
import com.v1.weibo4j.Weibo4jLO;

public class DwrAction extends BaseDwrAction {
	private String HTTP_BETALIFE_CN;
	private final Log log = LogFactory.getLog(getClass());
	private static final Pattern pattern = Pattern.compile("(?<=#)([^#]+)(?=#)");

	public DwrAction() {
		Constant constant = (Constant) Toolkit.getInstance().getBean("Constant");
		HTTP_BETALIFE_CN = constant.ADDRESS;
	}

	@Override
	public Object before() {
		Weibo4jLO agent = (Weibo4jLO) Toolkit.getInstance().getBean("Weibo4jLO");
		if (!agent.isLogin()) {
			return "没登录啊！？";
		}
		return null;
	}

	public String unfollowTrend(int trendId) {
		try {
			Weibo4jLO agent = (Weibo4jLO) Toolkit.getInstance().getBean("Weibo4jLO");
			agent.trendsDestroy(trendId);
			return null;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return "ops，失败了。>_<";
		}
	}

	public String invent(String statuses) {
		Matcher matcher = pattern.matcher(statuses);
		log.debug("statuses: " + statuses);
		int end = 0;
		String url = HTTP_BETALIFE_CN + "ourdiary/line.html?method.followTrend=";
		try {
			List<String> trends = new ArrayList<String>();
			while (end < statuses.length() && matcher.find(end)) {
				String trend = matcher.group(0);
				log.debug("trend: " + trend);
				trends.add(trend);
				url += "&trend=" + Toolkit.encodeZH(trend);
				// end is before second # position.
				end = 2 + matcher.end();
			}

			Weibo4jLO agent = (Weibo4jLO) Toolkit.getInstance().getBean("Weibo4jLO");
			String shorturl = agent.shorturl(url);
			String all = statuses + " " + shorturl;
			if (all.length() > 140) {
				return "内容有点长。>_<";
			}
			agent.UpdateStatus(all);

			for (String trend : trends) {
				if (!agent.isTrendFollowed(trend)) {
					agent.trendsFollow(trend);
				}
			}
			return null;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return "ops，失败了。>_<";
		}
	}

	@SuppressWarnings("unchecked")
	public List<User> getFriends(String trend) {
		Weibo4jLO agent = (Weibo4jLO) Toolkit.getInstance().getBean("Weibo4jLO");
		List<User> friends = agent.getFriends();
		MultiValueMap trendPaticipators = agent.getTrendPaticipator();
		if (!trendPaticipators.containsKey(trend)) {
			return friends;
		}

		Map<String, User> map = new HashMap<String, User>();
		for (User user : friends) {
			map.put(user.getId(), user);
		}
		Collection<User> paticipators = trendPaticipators.getCollection(trend);
		for (User user : paticipators) {
			map.remove(user.getId());
		}
		return new ArrayList<User>(map.values());
	}

}
